package com.hzsai.controller;

import com.hzsai.entity.Student;
import com.hzsai.repository.StudentRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * hzsai 2019/11/4
 * usage:
 **/
@Api(tags = "studentJPA")
@RestController

public class StudentController {

    @Autowired
    private StudentRepository studentRepository;

    @ApiOperation("findAll")
    @GetMapping(name = "haha", value = "/findAll")
    public List<Student> findAll() {
        return studentRepository.findAll();
    }

    @ApiOperation("findById")
    @GetMapping(value = "/findById/{id}")
    public Student findById(@PathVariable("id") Long id) {
        return studentRepository.findById(id).get();
    }

    @ApiOperation("add")
    @PostMapping(value = "/save")
    public Student save(@RequestBody Student student) {
        return studentRepository.save(student);
    }

    @ApiOperation("update")
    @PutMapping(value = "/update")
    public Student update(@RequestBody Student stu) {
        return studentRepository.save(stu);
    }

    @ApiOperation("deleteById")
    @DeleteMapping(value = "/deleteById/{}")
    public String delete(@PathVariable("id") Long id) {
        studentRepository.deleteById(id);
        return "hello, done";
    }


}
